package pl.koszolko.documentclustering.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import pl.koszolko.documentclustering.dao.Quotation;
import pl.koszolko.documentclustering.dao.QuotationDao;
import pl.koszolko.documentclustering.dao.utils.DataBaseConnectionManager;

public class JdbcQuotationDao implements QuotationDao {
	private static Logger logger = Logger.getLogger(JdbcAuthorDao.class);
	
	private static final String INSERT = "insert into CYTOWANIA VALUES(?,?)";
	private static final String DELETE = "delete from CYTOWANIA where ID_PUBLIKACJI = ? and ID_PUBLIKACJI_CYTOWANEJ = ?";
	private static final String FIND_ALL = "select ID_PUBLIKACJI, ID_PUBLIKACJI_CYTOWANEJ from CYTOWANIA order by ID_PUBLIKACJI";

	private DataBaseConnectionManager connectionManager;

	public JdbcQuotationDao(DataBaseConnectionManager connectionManager) {
		this.connectionManager = connectionManager;
	}
	
	@Override
	public void insert(Quotation quotation) {
		Connection conn = null;
		try {
			conn = connectionManager.getConnection();

			PreparedStatement ps = conn.prepareStatement(INSERT);
			ps.setLong(1, quotation.getDocumentId());
			ps.setLong(2, quotation.getDocumentCitedId());
			ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e2) {
			e2.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					logger.debug(e);
				}
		}
	}

	@Override
	public void delete(Long documentId, Long documentCitedId) {
		Connection conn = null;
		try {
			conn = connectionManager.getConnection();

			PreparedStatement ps = conn.prepareStatement(DELETE);
			ps.setLong(1, documentId);
			ps.setLong(2, documentCitedId);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e2) {
			e2.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					logger.debug(e);
				}
		}
	}

	@Override
	public List<Quotation> findAll() {
		List<Quotation> quotations = new ArrayList<Quotation>();
		Connection conn = null;
		try {
			conn = connectionManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(FIND_ALL);
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Quotation quotation = new Quotation();
				quotation.setDocumentId(rs.getLong(1));
				quotation.setDocumentCitedId(rs.getLong(2));
				quotations.add(quotation);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e2) {
			e2.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					logger.debug(e);
				}
		}

		return quotations;
	}

	public DataBaseConnectionManager getConnectionManager() {
		return connectionManager;
	}

	public void setConnectionManager(DataBaseConnectionManager connectionManager) {
		this.connectionManager = connectionManager;
	}

}
